package com.szu.subject.ui.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.szu.subject.domains.CourseInfo;
import com.szu.subject.domains.CourseStudent;
import com.szu.subject.domains.Selection;
import com.szu.subject.domains.StudentNum;
import com.szu.subject.ui.mapper.SelectionMapper;
import com.szu.subject.ui.service.SelectionService;

@Service
public class SelectionServiceImpl implements SelectionService {

	@Autowired
	private SelectionMapper selectionMapper;
	@Override
	public Map getSelectionByCourseId(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int removeCourses(Map map) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public void removeOverflow(List<CourseInfo> courseInfos) {
		List<Long> coursesId=courseInfos.stream().map(CourseInfo::getCourseId).collect(Collectors.toList());
		List<StudentNum> studentNums=selectionMapper.selectNum(coursesId);
		Map<Long, Map> numMap=new HashMap();
		Map<Boolean, Integer> temp;
		for(StudentNum num:studentNums){
			temp=new HashMap<Boolean,Integer>();
			numMap.put(num.getCourseId(), temp);
		}
		for(StudentNum num:studentNums){
			temp=numMap.get(num.getCourseId());
			temp.put(num.getType(), num.getSum());
		}
		
		List<CourseInfo> deleteInfo=new ArrayList<>();
		Map<Boolean,Integer> temp2;
		List<Long> sIds;
		List<Long> stuIds;
//		Map<String,Long>//未完成
		int sub,range,num,removeNum,total;
		Long tempId;
		Random rand = new Random();
		List<Long> subscript;
		List<CourseStudent> courseStudents=new ArrayList<CourseStudent>();
		CourseStudent courseStudent;
		for(CourseInfo courseInfo:courseInfos){
			temp2=numMap.get(courseInfo.getCourseId());
			total=temp2.get(false);
			sub=total-courseInfo.getPrimaryLimit();
			if(sub>0){//主选人数超过
				courseStudent=new CourseStudent();
				stuIds=selectionMapper.getPStudentIdByCourse(courseInfo.getCourseId());
				range=stuIds.size();//已有人数
				sIds=new ArrayList<>(sub);
				num=0;
//				sbscript=new ?
				while(num<range){
					removeNum=rand.nextInt(range);
					tempId=stuIds.get(removeNum);
					if(!sIds.contains(tempId)){
						sIds.add(tempId);
						num++;
					}
				}
				courseStudent.setCourseId(courseInfo.getCourseId());
				courseStudent.setStudentIds(sIds);
				courseStudents.add(courseStudent);
//				deleteInfo.add(courseInfo);
			}
			sub=temp2.get(true)-courseInfo.getPrimaryLimit();
			if(sub>0){//非主选人数超过
				courseStudent=new CourseStudent();
				stuIds=selectionMapper.getPStudentIdByCourse(courseInfo.getCourseId());
				range=stuIds.size();//已有人数
				sIds=new ArrayList<>(sub);
				num=0;
//				sbscript=new ?
				while(num<range){
					removeNum=rand.nextInt(range);
					tempId=stuIds.get(removeNum);
					if(!sIds.contains(tempId)){
						sIds.add(tempId);
						num++;
					}
				}
				courseStudent.setCourseId(courseInfo.getCourseId());
				courseStudent.setStudentIds(sIds);
				courseStudents.add(courseStudent);
			}
		}
		selectionMapper.removeOverflow(courseStudents);
	}

}
